package org.hk.kg.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LocalResouce {
	private static Log logger = LogFactory.getLog(LocalResouce.class);
	
	public static String fileJoin(String path,String file)
	{
		return path.concat(File.separator).concat(file);
	}
	public static InputStream getConfigFile(String fileName) throws FileNotFoundException
	{
		return new FileInputStream(new File(fileJoin(Constants.CONFIG_ROOT,fileName)));
	}
	
	public static InputStream getHtmlFile(String fileId) throws FileNotFoundException
	{
		return new FileInputStream(new File(fileJoin(Constants.PAGES_ROOT,fileId.concat(".html"))));
	}
	
	public static InputStream getPageFile(String file) throws FileNotFoundException
	{
		return new FileInputStream(new File(fileJoin(Constants.PAGES_ROOT,file)));
	}
	public static String getImageFile(String image)
	{
		return fileJoin(Constants.IMAGE_ROOT,image);
	}
	
	public static final String getTempImage(String image,String width,String height) throws IOException
	{
		File srcImage = new File(image);
		BufferedImage bi = ImageIO.read(srcImage);
		int imageWidth = bi.getWidth(),
			imageHeight = bi.getHeight();
		File tempImage = File.createTempFile(srcImage.getName(), Constants.IMAGE_TEMP_ROOT);
		tempImage.deleteOnExit();
		float stretch = getStretchRate(imageWidth, imageHeight, height, width);
		BufferedImage tag = new BufferedImage((int)(imageWidth*stretch), (int)(imageHeight*stretch),BufferedImage.TYPE_INT_BGR);
		tag.getGraphics().drawImage(bi,0,0,(int)(imageWidth*stretch), (int)(imageHeight*stretch),null);
		ImageIO.write(tag, "jpg", tempImage);
		return tempImage.getAbsolutePath();
	}
	
	public static URL getImage(String image) throws MalformedURLException
	{
		return new File(fileJoin(Constants.IMAGE_ROOT,image)).toURI().toURL();
	}
	
	public static void fileCopy(File srcFile,File targetFile,boolean force) throws IOException
	{
		InputStream ips = new FileInputStream(srcFile);
		OutputStream ops = new FileOutputStream(targetFile);
		if(targetFile.exists())
		{
			if(force)
			{
				targetFile.delete();
			}
			else
			{
				throw new IOException("File already exists!");
			}
		}
	
		byte[] bytes = new byte[1024];
		while(ips.read(bytes)!=-1)
		{
			ops.write(bytes);
		}
		ips.close();
		ops.close();
	}
	
	private static float getStretchRate(int width,int height,String targetWidth,String targetHeight)
	{
		if(StringUtils.isEmpty(targetWidth))
		{
			if(StringUtils.isEmpty(targetHeight))
			{
				return 1;
			}
			else
			{
				return Float.parseFloat(targetHeight)/height;
			}
		}
		else if(StringUtils.isEmpty(targetHeight))
		{
			if(StringUtils.isEmpty(targetWidth))
			{
				return 1;
			}
			else
			{
				return Float.parseFloat(targetWidth)/width;
			}
		}
		else
		{
			float widthStretch =  Float.parseFloat(targetWidth)/width,
				heightStretch = Float.parseFloat(targetHeight)/height;
			return widthStretch < heightStretch ? widthStretch:heightStretch;
		}
	}
}
